package com.example.common.utils;


import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

/**
 * 返回状态-消息
 *
 * @author
 * @date 2020/9/14
 */
public enum RestStatus {
	/**
	 * 系统状态
	 */
	SUCCESS(200, "success"),
	BAD_REQUEST(400, "提示"),
	UNAUTHORIZED(401, "用户身份认证失败，请登录后再操作"),
	FORBIDDEN(403, "用户无权限访问，请授权后再操作"),
	SYSTEM_ERROR(500, "系统内部错误，请联系运维人员处理"),
	FILE_UPLOAD_ERROR(501, "附件上传出错"),
	FILE_UPLOAD_FAIL(502, "附件上传失败"),
	WRONG_CLIENT_SECRET(1007, "wrong client secret"),

	/**
	 * 业务状态
	 */
	ENCODE_ERROR(5000, "encode转码异常"),
	BUSINESS_ERROR(5000, "业务错误"),
	SYS_PERMISSION_PERNAME_ERROR(5000, "名称不能为空"),
	SYS_PERMISSION_TYPE_ERROR(5000, "类型不能为空"),
	SYS_PERMISSION_APP_ERROR(5000, "所属应用不能为空"),
	SYS_PERMISSION_SORT_ERROR(5000, "排序不能为空"),
	SYS_PERMISSION_PERCODE_ERROR(5000, "编码不能为空"),
	SYS_PERMISSION_ERROR(5000, "菜单不能为空"),
	SYS_PERMISSION_CODE_ERROR(5000, "编码已存在"),
	ROLENAME_ERROR(5000, "角色名称已存在"),
	ROLEID_ERROR(5000, "角色不存在"),
	USER_ROLE_ERROR(5000, "当前选择用户已存在用户角色中"),
	USER_ROLE_ID_ERROR(5000, "选择用户不能为空"),
	BASE_PROJECT_ERROR(5000, "项目不存在"),
	BASE_PROJECT_PARA_INSERT(5000, "项目参数新增失败"),
	BASE_PROJECT_PARA_UPDATE(5000, "项目参数修改失败"),
	DATA_ERROR(5000, "数据异常"),

	SYS_DATA_ID_ERROR(5000, "业务id不存在"),

	NOT_NONE_PERMISSION(5000, "认证失败,找不到当前用户"),

	DELETE_GROUP_RESERVED_ERROR(5000, "删除失败，只能删除草稿状态的记录"),
	DELETE_RESERVED_ERROR(5000, "删除失败"),
	UPDATE_RESERVED_ERROR(5000, "更新失败"),
	EDIT_GROUP_RESERVED_ERROR(5000, "编辑失败，记录不存在"),
	MONTH_BUDGET_IMPORT(5201, "导入失败,请重新导入"),
	MONTH_BUDGET_SAVE(5000, "保存失败"),
	MONTH_BUDGET_SUBMIT(5000,"保存并提交失败"),
	;
	private final int code;

	private final String msg;

	RestStatus(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public int code() {
		return this.code;
	}

	public String msg() {
		return this.msg;
	}

	/**
	 * 根据消息解析
	 *
	 * @param msg 消息
	 * @author liqingdian
	 * @date 2020/9/14
	 */
	@Nullable
	public static RestStatus resolve(String msg) {
		RestStatus[] var1 = values();
		int var2 = var1.length;
		for (int var3 = 0; var3 < var2; ++var3) {
			RestStatus status = var1[var3];
			if (StringUtils.equals(status.msg, msg)) {
				return status;
			}
		}
		return RestStatus.SYSTEM_ERROR;
	}

	/**
	 * 动态消息
	 *
	 * @author liqingdian
	 * @date 2020/9/12
	 */
	public static String dynamicMsg(RestStatus status, String msg) {
		if (StringUtils.isBlank(msg)) {
			return status.msg();
		}
		return Joiner.on(": ").join(status.msg(), msg);
	}

	@Override
	public String toString() {
		return String.valueOf(this.code);
	}
}

